//
//  FLAlertViewController.m
//  UIAlertC
//
//  Created by 范琦 on 16/5/24.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "FLAlertViewController.h"
#import "AppDelegate.h"

@interface FLAlertViewController ()

@end

@implementation FLAlertViewController

static FLAlertViewController *_shareAlert = nil;



/**
        *****FLAlertViewController*****
    参数1: 标题
    参数2: 信息
    参数3: 显示style
    参数4: 数组, 存放类型UIAlertAction. 控制Alert的点击事件
    参数5: 延迟时间  当有需求时, 使AlertController延迟几秒自动dismiss. 弱不需要则设为0
**/

+ (instancetype)alertControllerWithTitle:(NSString *)title
                                 message:(NSString *)message
                        preferredStyle:(UIAlertControllerStyle)preferredStyle
                               actionArr:(NSArray *)arr dismissTimer:(float)time
{
    //线程锁
    @synchronized(self) {
        
        _shareAlert = [super alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
        
        // 给alert添加action
        
        for (UIAlertAction *act in arr) {
            [_shareAlert addAction:act];
        }
        
        // 获取当前的window 自动弹出
        
        UIWindow *window = [[UIApplication sharedApplication].delegate window];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01), dispatch_get_main_queue(), ^{
                
                [window.rootViewController presentViewController:_shareAlert animated:NO completion:nil];
        });
        
        // 自动dismiss
        
        
        if (time != 0) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, time * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                
                [_shareAlert dismissViewControllerAnimated:YES completion:nil];
            });
        }
        return _shareAlert;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidDisappear:(BOOL)animated
{
    if (_shareAlert != nil) {
        _shareAlert = nil;
    }

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
